<?php

namespace app\common\validate\salesman;

use app\common\model\shop\Category;
use app\common\utils\Utils;
use app\common\validate\BaseValidate;

class RegisterValidate extends BaseValidate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'name|客户姓名' => 'require|length:2,200',
        'mobile|客户电话' => 'require|isMobile',
        'company_name|牛场名称' => 'require|length:2,200',
        'address|牛场地址' => 'require|length:2,400',
        'json_data|品种&数量' => 'require|checkJson',
        'other|其他信息' => 'max:800',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];


    public function checkJson($value) {
        $arr = Utils::explodeStr($value);
        if (empty($arr)) {
            return '牛的品种和数量不能为空';
        }


        foreach ($arr as $k => $v) {
            $info = Utils::explodeStr($v, '_');
            $pinzhong = $info[0];
            $num = $info[1];
            if (!is_numeric($pinzhong) || !is_numeric($num) || $num == 0) {
                return '牛的数量不能为空';
            }

            //$k 为牛的分类
            $res = \app\common\model\custom\Category::get($pinzhong);
            if (!$res) {
                return '请选择牛的品种';
            }
        }

        return true;
    }


}
